package com.service.tools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;

public class RSAUtils {

	private static String ALGORITHM = "RSA";

	/**
	* 解密算法
	* cryptograph:密文
	*/
	public static String decrypt(String cryptograph, InputStream is) throws Exception{
	   /** 将文件中的私钥对象读出 */
	   ObjectInputStream ois = new ObjectInputStream(is);
	   Key key = (Key) ois.readObject();
	   /** 得到Cipher对象对已用公钥加密的数据进行RSA解密 */
	   Cipher cipher = Cipher.getInstance(ALGORITHM);
	   cipher.init(Cipher.DECRYPT_MODE, key);
	   BASE64Decoder decoder = new BASE64Decoder();
	   byte[] b1 = decoder.decodeBuffer(cryptograph);
	   /** 执行解密操作 */
	   byte[] b = cipher.doFinal(b1);
	   return new String(b);
	}
	
	/**
	 * 获取本机mac地址
	 * @param ia
	 * @return
	 * @throws SocketException
	 */
	public static String getLocalMac(InetAddress ia) throws SocketException {
		byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
		StringBuffer sb = new StringBuffer("");
		for(int i=0; i<mac.length; i++) {
			if(i!=0) {
				sb.append("-");
			}
			int temp = mac[i]&0xff;
			String str = Integer.toHexString(temp);
			if(str.length()==1) {
				sb.append("0"+str);
			}else {
				sb.append(str);
			}
		}
		return sb.toString().toUpperCase();
	}
	
	 /** 
     * 获取unix网卡的mac地址. 非windows的系统默认调用本方法获取. 
     * 如果有特殊系统请继续扩充新的取mac地址方法. 
     *  
     * @return mac地址 
     */  
    public static Map<String, String> getUnixMACAddress(InetAddress ip) throws Exception {
    	String command = "/bin/sh -c ifconfig -a";
    	Process p = Runtime.getRuntime().exec(command);
		BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
		String line = null, mac = null, inet = null;
		Map<String, String> map = new HashMap<String, String>();
		String macRegx1 = "(?<mac>[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5})";
		String macRegx2 = "(?<mac>[0-9a-fA-F]{2}(-[0-9a-fA-F]{2}){5})";
		String ipRegx = "inet addr:(?<ip>(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9]))";
		Pattern pat = null;
		Matcher m = null;
    	try {
    		while ((line = br.readLine()) != null) {
    			
    			if (line.indexOf("HWaddr") > 0) {
    				try {
    					pat = Pattern.compile(macRegx1);
    					m = pat.matcher(line);
    					if (m.find()) {
    						mac = m.group("mac");
    					} else {
    						pat = Pattern.compile(macRegx2);
        					m = pat.matcher(line);
        					if (m.find())
        						mac = m.group("mac");
    					}
    				} catch (Exception e) {
    					
    				}
    			}
    			
    			if (line.indexOf("inet addr") > 0) {
    				try {
    					pat = Pattern.compile(ipRegx);
    					m = pat.matcher(line);
    					if (m.find())
    						inet = m.group("ip");
    				} catch (Exception e) {
    					
    				}
    			}
    			
    			if (null != mac && !"".equals(mac) && null != inet && !"".equals(inet)) {
    				map.put(mac, inet);
    			}
    			
    			if (line.indexOf("Interrupt") > 0) {
    				mac = null;
    				inet = null;
    				continue;
    			}
    		}
    		if (map.size() > 0)
    			return map;
    		else 
    			return null;
    	} finally {
    		br.close();
    	}
    }
	
    /** 
     * 获取当前操作系统名称. return 操作系统名称 例如:windows xp,linux 等. 
     */  
    public static String getOSName() {  
        return System.getProperty("os.name").toLowerCase();  
    }
    
}
